#include "blockgroupdes.h"

BlockGroupDes::BlockGroupDes(char *buf)
{
    bg_free_blocks_count = Superblock::char2int(buf, 12);
    bg_free_inodes_count = Superblock::char2int(buf, 14);
    bg_block_bitmap = Superblock::char4int(buf, 0);
}

void BlockGroupDes::stat(QFile *file, int blockSize, int blocks)
{
    c<<"\t\tNumer blok z bitmapą bloków: "<<bg_block_bitmap<<e;
    c<<"\t\tLiczba wolnych i-węzłów w grupie: "<<bg_free_inodes_count<<e;
    c<<"\t\tLiczba wolnych bloków w grupie: "<<bg_free_blocks_count<<e;
    getBitmap( file, blockSize, blocks);
}

void BlockGroupDes::getBitmap(QFile *file, int blockSize, int blocks)
{
    qint64 pos = file->pos();
    int one, ones =0, zeros=0;
    char bitmap[blockSize];
    file->seek((bg_block_bitmap)*blockSize);
    file->read(bitmap,blockSize);
    c<<"\tInformacje o bitmapie bloków: "<<e;
    for(int i =0;  i< blockSize; i++)
    {
        one=count_1bits(bitmap[i]);
        ones+=8-one;
        zeros += one;
    }
    if(blocks<0)
    {
        ones+=blocks;
    }
    c<<"\t\tLiczba 1 w bitmapie: "<<ones<<"\n\t\tLiczba 0 w bitmapie: "<<zeros<<e;
    file->seek(pos);
}
unsigned char BlockGroupDes::count_1bits(unsigned char x)
{
    size_t num_zeroes = 0;
    for(size_t i = 0; i < CHAR_BIT * sizeof x; ++i, x >>= 1)
    {
      if ((x & 1) == 0)
        ++num_zeroes;
    }
    return num_zeroes;
}
